import axios from 'axios'

import { useRouter } from 'vue-router'

const use = useRouter() ;


const instance = axios.create({
     baseURL:"http://localhost:5173/api",
     timeout:5000
})

instance.interceptors.request.use(function(config){
    if(config.url.indexOf("Login") === -1 && config.url.indexOf("register") === -1 && config.url.indexOf("getArticle") === -1 && config.url.indexOf("getComment")=== -1 && config.url.indexOf("findComment")=== -1 && config.url.indexOf("login") === -1 && config.url.indexOf("image") === -1 && config.url.indexOf("carousel") == -1)
    config.headers.Authorization = "Bearer "+ localStorage.getItem("jwttoken");
    if(config.url.indexOf("update") != -1 || config.url.indexOf("delete") != -1 || config.url.indexOf("add") != -1)
    config.headers['ConfirmUserId'] = localStorage.getItem("userId");
    if(config.url.indexOf("image")===-1)
    {
        config.headers['Content-Type'] = "application/json"

    }
    return config ; 
},
function (response) {   
    return response ;
},
function(error)
{
    if(error.response.status == 401)
    {
        return use.push("/login")
    }
}
)

export default instance;